package ModelLayer;
import java.util.*;//Used for ArrayList

/**
 * Write a description of class LoanList here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanList
{   public ArrayList<Loan> loan ;
    //Instance variable ArrayList for 'Loans'
    /**
     * Constructor
     */
    public LoanList()
    { 
        loan = new ArrayList<Loan>();
    }

    /**
     * Methods for LoanList Array: add,return all, find loan(using the copy borrowed),find loan(using the unique ID)
     * We did not add a remove loan as we will set the status of the loan that is returned as 'Returned' and we keep it for history records.
     */
    public void addLoad(Loan _newLoan)
    {
        loan.add(_newLoan);
    }

    public ArrayList<Loan> getLoanList()
    {
        return loan;
    }

    public Loan findLoanViaCopy(Copy _c)
    {
        Loan tempLoan = null;
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        int i= 0;
        boolean found= false;
        while ( !found && i<loan.size())
        {
            Loan l = loan.get(i);
            if (l.getCopy() == _c && l.getState().equals("Loaned"))
            {
                tempLoan = l;
                found= true;
            }
            else
                i++;
        }
        return tempLoan;
    }

    public Loan findLoan(long _id)
    {
        Loan tempLoan = null;
        int i= 0;
        boolean found= false;
        while ( !found && i<loan.size())
        {
            Loan l = loan.get(i);
            if (l.getID() == _id)
            {
                tempLoan = l;
                found= true;
            }
            else
                i++;
        } // end while

        return tempLoan;
    }
}
